package com.migo.service;

import com.migo.pojo.CatNode;
import com.migo.pojo.ItemCat;
import com.migo.pojo.ItemCatResult;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ItemCatService extends BaseService<ItemCat>{
    public ItemCatResult getItemCatList(){
        ItemCatResult itemCatResult = new ItemCatResult();
        List catList = getItemCatList(0L);
        itemCatResult.setCatNodes(catList);
        return itemCatResult;
    }

    public List getItemCatList(long parentId){
        ItemCat example = new ItemCat();
        example.setParentId(parentId);
        List<ItemCat> catList = queryListByWhere(example);
        List resultList = new ArrayList();
        int count = 0;
        for(ItemCat itemCat : catList){
            if(itemCat.getIsParent()){
                CatNode node = new CatNode();
                node.setUrl("/products/" + itemCat.getId() + ".html");
                if(itemCat.getParentId() == 0){
                    node.setName("<a href='/products/" + itemCat.getId() + ".html'>" +itemCat.getName() + "</a>");
                }else{
                    node.setName(itemCat.getName());
                }
                node.setItems(getItemCatList(itemCat.getId()));
                resultList.add(node);
                count++;
                if(parentId==0&&count>=14){
                    break;
                }
            }else{
                String item = "/products/" + itemCat.getId() + ".html|" + itemCat.getName();
                resultList.add(item);
            }
        }
        return resultList;
    }

}
